#include "BetterFullCrossover.h"
#include "EvolutionaryAlgorithm.h"
#include "Printer.h"

#include <set>

EvolutionaryAlgorithm::population_type BetterFullCrossover::cross(Genotype & gen1, Genotype & gen2)
{
	double val1 = gen1.fitnessValue;
	double val2 = gen2.fitnessValue;
	Genotype child(gen1.graph);
	if (val1 > val2)
	{
		child.gens = gen1.gens;
		updateChild(child, gen1, gen2);
	}
	else
	{
		child.gens = gen2.gens;
		updateChild(child, gen2, gen1);
	}

	EvolutionaryAlgorithm::population_type result;
	result.insert(child);
	//if (child.getVSize() > gen1.getVSize() && child.getVSize() > gen2.getVSize())
	//{
	//	std::cout<<"krzyzuje"<<gen1 << gen2<<"o fitness " <<val1 << " " << val2;
	//	std::cout<<"wyszlo ";
	//	std::cout<<child<<std::endl;
	//}
	return result;
}

void BetterFullCrossover::updateChild(Genotype & child, Genotype & gen1, Genotype & gen2)
{
    //std::cout << "spr verts: ";
	std::set<int> vertsToCheck = gen1.getNotContained(gen2);
    //Printer::ciout(vertsToCheck);
	for (std::set<int>::const_iterator vertIter = vertsToCheck.begin();
		vertIter != vertsToCheck.end();
		++vertIter)
	{
		if (gen1.isConnectedToAll(*vertIter))
		{
			child.set(*vertIter);
		}
	}
	child.recalculateFitnessValue();
}